package com.ruoyi.project.cwsk.channel.service.impl;

import java.util.List;

import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.project.cwsk.channel.domain.Channel;
import com.ruoyi.project.cwsk.channel.mapper.ChannelMapper;
import com.ruoyi.project.cwsk.channel.service.IChannelService;
import com.ruoyi.project.system.post.domain.Post;
import com.ruoyi.project.system.post.service.IPostService;
import com.ruoyi.project.system.role.domain.Role;
import com.ruoyi.project.system.role.service.IRoleService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;

import javax.annotation.Resource;

/**
 * 渠道商管理Service业务层处理
 * 
 * @author songzc
 * @date 2019-12-27
 */
@Service
public class ChannelServiceImpl implements IChannelService {
	@Resource
	private ChannelMapper channelMapper;
	
	@Autowired
	private IRoleService roleService;
	
	@Autowired
	private IPostService postService;
	
	@Autowired
	private IUserService userService;

	/**
	 * 查询渠道商管理
	 * 
	 * @param id 渠道商管理ID
	 * @return 渠道商管理
	 */
	@Override
	public Channel selectChannelById(Integer id) {
		return channelMapper.selectChannelById(id);
	}

	/**
	 * 查询渠道商管理列表
	 * 
	 * @param channel 渠道商管理
	 * @return 渠道商管理
	 */
	@Override
	@DataSource(DataSourceType.SLAVE)
	public List<Channel> selectChannelList(Channel channel) {
		return channelMapper.selectChannelList(channel);
	}

	/**
	 * 新增渠道商管理
	 * 
	 * @param channel 渠道商管理
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertChannel(Channel channel) {
		// 保存关联的帐号信息
		User user = channel.getUser();
		if (user != null) {
			user.setUserName(channel.getContact());
			user.setStatus(UserConstants.NORMAL);;
			user.setAcronym(channel.getAcronym());

			Long[] roleIds = new Long[1];
			Long[] postIds = new Long[1];
			// 默认角色、岗位
			Role role = null;
			Post post = null;
			if ("AGENT".equals(channel.getModel())) {
				role = roleService.findByRoleKey("ROLE_AGENT");
				post = postService.selectPostByCode("agent");
			} else {
				role = roleService.findByRoleKey("ROLE_CHANNEL");
				post = postService.selectPostByCode("channel");
			}
			roleIds[0]=role.getRoleId();
			postIds[0]=post.getPostId();
			
			user.setRoleIds(roleIds);
			user.setPostIds(postIds);

			userService.insertUser(user);
		}
		return channelMapper.insertChannel(channel);
	}

	/**
	 * 修改渠道商管理
	 * 
	 * @param channel 渠道商管理
	 * @return 结果
	 */
	@Override
	public int updateChannel(Channel channel) {
		return channelMapper.updateChannel(channel);
	}

	/**
	 * 删除渠道商管理对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deleteChannelByIds(String ids) {
		return channelMapper.deleteChannelByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除渠道商管理信息
	 * 
	 * @param id 渠道商管理ID
	 * @return 结果
	 */
	@Override
	public int deleteChannelById(Long id) {
		return channelMapper.deleteChannelById(id);
	}

	/**
	 * 校验渠道名称是否唯一
	 * 
	 * @param name 名称
	 * @return 结果
	 */
	@Override
	public String checkNameUnique(Channel channel) {

		Long channelId = StringUtils.isNull(channel.getId()) ? -1L : channel.getId();
		Channel info = channelMapper.checkNameUnique(channel.getName());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != channelId.longValue())
        {
            return UserConstants.USER_NAME_NOT_UNIQUE;
        }
        return UserConstants.USER_NAME_UNIQUE;
	}
	
	/**
	 * 校验渠道名称缩写是否唯一
	 * 
	 * @param acronym 缩写
	 * @return 结果
	 */
	@Override
	public String checkAcronymUnique(Channel channel) {
		Long channelId = StringUtils.isNull(channel.getId()) ? -1L : channel.getId();
		Channel info = channelMapper.checkAcronymUnique(channel.getAcronym());
        if (StringUtils.isNotNull(info) && info.getId().longValue() != channelId.longValue())
        {
        	return UserConstants.USER_NAME_NOT_UNIQUE;
        }
        return UserConstants.USER_NAME_UNIQUE;
	}

	/**
     * 渠道状态修改
     * 
     * @param channel 渠道信息
     * @return 结果
     */
	@Override
	public int changeStatus(Channel channel) {
		return channelMapper.updateChannel(channel);
	}

	@Override
	public Channel findChannelByUser(Long userId) {
		Channel channel = channelMapper.findByUserId(userId);
		if(channel == null)
			channel = channelMapper.findBySubUserId(userId);
		return channel;
	}

	@Override
	public Channel findBySubUser(Long userId) {
		Channel channel = channelMapper.findBySubUserId(userId);
		if(channel == null)
			channel = channelMapper.findBySubUserTwoStep(userId);
		return channel;
	}

	@Override
	public List<Channel> loadChannels(Channel channel) {
		return channelMapper.loadChannels(channel);
	}

	@Override
	public List<Integer> findChannelIdByPromoterId(Long userId) {
		return channelMapper.findChannelIdByPromoterId(userId);
	}
}
